﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InterviewPractice
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            LinkedLists();
            SetOfStacksTesting();
        }


        public class SetOfStacks<T>
        {
            List<Stack<T>> m_ListOfStacks = new List<Stack<T>>();
            int m_MaxStackSize;

            int m_Count = 0;
            public int Count
            {
                get { return m_Count; }
            }

            public SetOfStacks(int maxStackSize)
            {
                m_MaxStackSize = maxStackSize;
            }

            Stack<T> AddNewStack()
            {
                Stack<T> newStack = new Stack<T>();
                m_ListOfStacks.Add(newStack);
                return newStack;
            }

            void RemoveStack()
            {
                int lastIndex = m_ListOfStacks.Count - 1;
                if (lastIndex >= 0)
                {
                    m_ListOfStacks.RemoveAt(lastIndex);
                }
            }

            Stack<T> GetTopStack()
            { 
                int lastIndex = m_ListOfStacks.Count - 1;
                if (lastIndex >= 0)
                {
                    return m_ListOfStacks[lastIndex];
                }

                return null;
            }

            public void Push(T item)
            {
                Stack<T> topStack = GetTopStack();
                if (topStack == null ||
                    topStack.Count >= m_MaxStackSize)
                {
                    topStack = AddNewStack();
                }

                topStack.Push(item);
                m_Count++;
            }

            public T Peek()
            {
                Stack<T> topStack = GetTopStack();
                if (topStack != null)
                {
                    return topStack.Peek();
                }

                return default(T);
            }

            public T Pop()
            {
                Stack<T> topStack = GetTopStack();
                if (topStack != null)
                {
                    if (topStack.Count == 0)
                    {
                        RemoveStack();
                        return this.Pop();
                    }

                    m_Count--;
                    return topStack.Pop();
                }

                return default(T);
            }

            //public T PopAt(int index)
            //{
            //}
        }

        void SetOfStacksTesting()
        {
            SetOfStacks<int> setOfStacks = new SetOfStacks<int>(3);
            for (int i = 0; i < 10; i++)
            {
                setOfStacks.Push(i);
            }

            while (setOfStacks.Count > 0)
            {
                int value = setOfStacks.Pop();
                textBox1.Text += value + " ";
            }

            return;
        }

        bool LinkedLists()
        {
            LinkedList<char> list = new LinkedList<char>();
            list.AddLast('a');
            list.AddLast('b');
            list.AddLast('c');
            list.AddLast('b');
            list.AddLast('a');

            LinkedListNode<char> slowNode = list.First;
            LinkedListNode<char> fastNode = list.First;
            
            Stack<LinkedListNode<char>> stack = new Stack<LinkedListNode<char>>();
            //stack.push

            int count = 0;
            while (fastNode != null)
            {
                fastNode = fastNode.Next;
                count++;

                if (count % 2 == 0)
                {
                    stack.Push(slowNode);
                    slowNode = slowNode.Next;
                }
            }

            if (count % 2 != 0)
            {
                slowNode = slowNode.Next;
            }

            while (slowNode != null)
            {
                if (slowNode.Value != stack.Pop().Value)
                {
                    return false;
                }
                slowNode = slowNode.Next;
            }

            return true;
        }
    }
}
